//
//  FHEmoticonCollectionViewCell.swift
//  WeiBo
//
//  Created by qfh on 16/7/19.
//  Copyright © 2016年 qiufuhe. All rights reserved.
//

import UIKit

class FHEmoticonCollectionViewCell: UICollectionViewCell {
    
    
 /// 数组
    var emoticon: [FHEmoticon]? {
    
        
        didSet {
            
            guard let ets = emoticon else {
                return
            }
            //  隐藏全部按钮
            for value in buttonArray {
                value.hidden = true
            }
            
            buttonArray.last?.hidden = false
            
            for (i, value) in ets.enumerate() {
                //  获取表情按钮
                let button = buttonArray[i]
                
                button.emoticon = value
                //  设置数据显示按钮
                button.hidden = false
                if value.type == "0" {
                    //  设置图片
                    //let image = UIImage(named: value.png!)
                    button.setImage(UIImage(named: value.path!), forState: .Normal)
                    button.setTitle(nil, forState: .Normal)
                } else {
                    //  设置emoji 
                    button.setTitle((value.code! as NSString).emoji(), forState: .Normal)
                    button.setImage(nil, forState: .Normal)
                }
                
                
                
            }
        }
    
    
    
    }
    /// button 数组
    private lazy var buttonArray: [FHEmoticonButton] = [FHEmoticonButton]()
    
    
    private lazy var deleteEmoticonButton: FHEmoticonButton = {
    
        let button = FHEmoticonButton()
        
        button.setImage(UIImage(named: "compose_emotion_delete"), forState: .Normal)
        
        button.setImage(UIImage(named: "compose_emotion_delete_highlighted"), forState: .Highlighted)
        
        button.addTarget(self, action: "deleteButton", forControlEvents: .TouchUpInside)
        
        self.contentView.addSubview(button)
    
        return button
    }()
    
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    private func setupUI() {
        
        contentView.addSubview(deleteEmoticonButton)
        
        
        for _ in 0..<20 {
            let button = FHEmoticonButton()
            button.titleLabel?.font = UIFont.systemFontOfSize(34)
            contentView.addSubview(button)
            buttonArray.append(button)
            
            button.addTarget(self, action: "selcateButton:", forControlEvents: .TouchUpInside)
        }
        
        buttonArray.append(deleteEmoticonButton)
        
        
        
        
       
        
        
    }
    
    override func layoutSubviews() {
        super.layoutSubviews()
        
        let itemWidth = width / 7
        
        let itemheight = height / 3
        
        for (i, value) in buttonArray.enumerate() {
        
        
            let col: CGFloat = CGFloat(i % 7 )
            
            let row: CGFloat = CGFloat(i / 7)
            
            let itemX = col * itemWidth
            
            let itemY = row * itemheight
        
            value.frame = CGRectMake(itemX, itemY, itemWidth, itemheight)
        
            
        }
        
        
        
        
        
        
        
    }
    
    
    // MARK: - 按钮点击  发送通知
    
    
    @objc private func deleteButton() {
        
      
        
        NSNotificationCenter.defaultCenter().postNotificationName(DidSelecteDeleteEmoticonButtonNotification, object: nil)
        
    }
    
    @objc private func selcateButton(btn: FHEmoticonButton) {
        

        let emoticon = btn.emoticon!
        
        NSNotificationCenter.defaultCenter().postNotificationName(DidSeletedEmotionButtonNotification, object: emoticon)
        
        
    }
    
    
    deinit {
    
        NSNotificationCenter.defaultCenter().removeObserver(self)
    
    }
    
}
